package com.project.common.domain;
import com.project.common.domain.po.BaseAutoKeyPO;
import com.project.common.domain.po.BasePrimaryKeyPO;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.criteria.Predicate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.domain.Specification;
import java.util.ArrayList;
import java.util.List;

/**
 */
@Data
@MappedSuperclass
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners({DataScopeAware.class})
public class DataScopeMatcher extends BaseAutoKeyPO {

    private String orgKey;

    private String roleKeys;

    private Long createBy;

    public static ExampleMatcher build() {
        //创建匹配器，即如何使用查询条件
        ExampleMatcher exampleMatcher = ExampleMatcher
                //采用默认and的查询方式
                .matchingAll()
                //忽略大小写
                .withIgnoreCase()
                //忽略所有null值的字段
                .withIgnoreNullValues()
                //.withIgnorePaths("id","createDate")
                //默认采用精准匹配规则
                .withStringMatcher(ExampleMatcher.StringMatcher.EXACT)
                //级联查询，字段user.email采用字符前缀匹配规则
                .withMatcher("roleKeys", ExampleMatcher.GenericPropertyMatchers.startsWith())
                //.withMatcher("roleKeys", ExampleMatcher.GenericPropertyMatchers.endsWith())
                //特殊指定address字段采用后缀匹配
                //.withMatcher("orgKey", ExampleMatcher.GenericPropertyMatchers.endsWith());
                .withMatcher("orgKey", ExampleMatcher.GenericPropertyMatchers.startsWith());
        return exampleMatcher;
    }

    public Specification spec() {
        return (root, criteriaQuery, criteriaBuilder) -> {
            criteriaQuery.distinct(true);
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("isDelete"), 0));
            //部门及以下
            if (StringUtils.isNotBlank(this.getOrgKey())) {
                predicates.add(criteriaBuilder.like(root.get("orgKey"), this.getOrgKey() + "%"));
            }
            //岗位及以下
            if (StringUtils.isNotBlank(this.getRoleKeys())) {
                String[] roleKeys = this.getRoleKeys().split(",");
                if (roleKeys.length > 0) {
                    Predicate[] keys = new Predicate[roleKeys.length];
                    for (int i = 0; i < roleKeys.length; i++) {
                        if (StringUtils.isNotBlank(roleKeys[i])) {
                            keys[i] = criteriaBuilder.like(root.get("roleKeys"), roleKeys[i] + '%');
                        }
                    }
                    predicates.add(criteriaBuilder.or(keys));
                }
            }
            //本人的数据
            if (this.getCreateBy() != null) {
                predicates.add(criteriaBuilder.equal(root.get("createBy"), this.getCreateBy()));
            }
            Predicate[] predicateArray = new Predicate[predicates.size()];
            return criteriaBuilder.and(predicates.toArray(predicateArray));
        };
    }
}
